<?php
/**
 * Classe/OperationDate.php
 */

namespace Operationtest\Classe;

use Zend\Session\Container;

class OperationDate {
    /**
     * Get custom period by entering a speicific date
     * @param \DateTime $begin
     * @return array $period
     */
    public function getCustomPeriod($begin) {
        $period = array();
        $begin = new \DateTime($begin);
        $interval = new \DateInterval('P1D');
        $end = clone $begin;
        $end->modify('first day of next month');
        $jours = new \DatePeriod($begin,$interval,$end);
        foreach($jours as $jour) {
            array_push($period, $jour->format('Y-m-d'));
        }
        return $period;
    }
    
    /**
     * Get all period from selection
     * @param array $request
     * @return array $period
     */
    public function displayPeriod($request) {
        $period = array();
        $periodY = array(date('Y'));
        $periodM = array(date('m'));
        $semaine = array(1,2,3,4,5,6,0);
        
        if($request->getPost()->periodY) { $periodY = $request->getPost()->periodY; }
        if($request->getPost()->periodM) { $periodM = $request->getPost()->periodM; }
        if($request->getPost()->semaine) { $semaine = $request->getPost()->semaine; }
        
        foreach($request->getPost()->periodY AS $year):
            foreach($request->getPost()->periodM AS $month):
                $month = ($month<10)?'0'.$month:$month;
                $begin = new \DateTime($year.'-'.$month.'-01');
                $interval = new \DateInterval('P1D');
                $end = clone $begin;
                $end->modify('first day of next month');
                $jours = new \DatePeriod($begin, $interval, $end);
                foreach($jours AS $jour):      
                    $mois = $this->echoMonthText(intval($month)).' '.$year;
                    $semaine = $jour->format('W');
                    $dayText = $this->echoLongDayText($jour->format('w'));
                    $period[$mois][$semaine][$dayText] = $jour;
                endforeach;
            endforeach;
        endforeach;
        $operationSession = new Container('operationSession');
        $operationSession->period = $period;
        return $period;
    }
    
    /**
     * Get all period from selection
     * @param array $request
     * @return array $period
     */
    public function displayPeriod2($request) {
        $period = array();
        $periodY = array(date('Y'));
        $periodM = array(date('m'));
        $semaine = array(1,2,3,4,5,6,0);
        
        if($request->getPost()->periodY) { $periodY = $request->getPost()->periodY; }
        if($request->getPost()->periodM) { $periodM = $request->getPost()->periodM; }
        if($request->getPost()->semaine) { $semaine = $request->getPost()->semaine; }
        
        foreach($request->getPost()->periodY AS $year):
            foreach($request->getPost()->periodM AS $month):
                $month = ($month<10)?'0'.$month:$month;
                $begin = new \DateTime($year.'-'.$month.'-01');
                $interval = new \DateInterval('P1D');
                $end = clone $begin;
                $end->modify('first day of next month');
                $jours = new \DatePeriod($begin, $interval, $end);
                foreach($jours AS $jour):      
                    $mois = $year.'-'.$month; // Ce qui est different avec displayPeriod($request)
                    $semaine = $jour->format('W');
                    $dayText = $this->echoLongDayText($jour->format('w'));
                    $period[$mois][$semaine][$dayText] = $jour;
                endforeach;
            endforeach;
        endforeach;
        $operationSession = new Container('operationSession');
        $operationSession->period2 = $period;
        return $period;
    }
    
    /**
     * Display period text from selection
     * @param array $request
     * @return string $periodText
     */    
    public function displayPeriodText($request) {
        $periodText = '';
        $periodY = array(date('Y'));
        $periodM = array(date('m'));
        $semaine = array(1,2,3,4,5,6,0);
        
        if($request->getPost()->periodY) { $periodY = $request->getPost()->periodY; }
        if($request->getPost()->periodM) { $periodM = $request->getPost()->periodM; }
        if($request->getPost()->semaine) { $semaine = $request->getPost()->semaine; }
        
        $countMonth = 1;
        foreach($periodM as $month):
            if($countMonth == 1):
                $periodText .= $this->echoMonthText(intval($month));
            else:
                $periodText .= ', '.$this->echoMonthText(intval($month));
            endif;
            $countMonth++;
        endforeach;
        
        $periodText .= ' '.implode(',', $periodY);
        $periodText .= ' - ';
        
        if(isset($request->getPost()->altDate) && $request->getPost()->altDate != "") {
            $specDate = new \DateTime($request->getPost()->altDate);
            $tmestamp = $specDate->getTimestamp();
            $dText = $this->echoShortDayText(intval(date('w',$tmestamp)));
            $dNum = date('d',$tmestamp);
            $mText = $this->echoMonthText(date('n',$tmestamp));
            $yText = date('Y',$tmestamp);
            $periodText .= $dText.' '.$dNum.' '.$mText.' '.$yText;
        } else {
            $countDay = 1;
            foreach($semaine as $day):
                if($countDay == 1):
                    $periodText .= $this->echoShortDayText($day);
                else:
                    $periodText .= ' '.$this->echoShortDayText($day);
                endif; 
                $countDay++;
            endforeach;
        }
        
        return $periodText;
    }
    
    /**
     * Get short text of day
     * @param int $dayNum
     * @return string $shortDayText
     */
    public function echoShortDayText($dayNum) {
        $dayText[0] = 'dim';
        $dayText[1] = 'lun';
        $dayText[2] = 'mar';
        $dayText[3] = 'mer';
        $dayText[4] = 'jeu';
        $dayText[5] = 'ven';
        $dayText[6] = 'sam';        
        
        $shortDayText = $dayText[$dayNum];
        return $shortDayText;
    }
    
    /**
     * Get long text of day
     * @param int $dayNum
     * @return string $longDayText
     */
    public function echoLongDayText($dayNum) {
        $dayText[0] = 'dimanche';
        $dayText[1] = 'lundi';
        $dayText[2] = 'mardi';
        $dayText[3] = 'mercredi';
        $dayText[4] = 'jeudi';
        $dayText[5] = 'vendredi';
        $dayText[6] = 'samedi';        
        
        $longDayText = $dayText[$dayNum];
        return $longDayText;
    }
    
    /**
     * Get long text of month
     * @param int $monthNum
     * @return string $longMonthText
     */
    public function echoMonthText($monthNum) {
        $monthText[1] = 'Janvier';
        $monthText[2] = 'Février';
        $monthText[3] = 'Mars';
        $monthText[4] = 'Avril';
        $monthText[5] = 'Mai';
        $monthText[6] = 'Juin';
        $monthText[7] = 'Juillet';
        $monthText[8] = 'Août';
        $monthText[9] = 'Septembre';
        $monthText[10] = 'Octobre';
        $monthText[11] = 'Novembre';
        $monthText[12] = 'Décembre';
        
        $longMonthText = $monthText[$monthNum];
        return $longMonthText;
    }
    
    /**
     * Get month text in array
     * @return array $monthText
     */
    public function tabMonthText() {
        $monthText[1] = 'Janvier';
        $monthText[2] = 'Février';
        $monthText[3] = 'Mars';
        $monthText[4] = 'Avril';
        $monthText[5] = 'Mai';
        $monthText[6] = 'Juin';
        $monthText[7] = 'Juillet';
        $monthText[8] = 'Août';
        $monthText[9] = 'Septembre';
        $monthText[10] = 'Octobre';
        $monthText[11] = 'Novembre';
        $monthText[12] = 'Décembre';
        
        return $monthText;
    }
    
    public function echoMonthtoFr() {
        $monthText['January'] = 'Janvier';
        $monthText['February'] = 'Février';
        $monthText['March'] = 'Mars';
        $monthText['April'] = 'Avril';
        $monthText['May'] = 'Mai';
        $monthText['June'] = 'Juin';
        $monthText['July'] = 'Juillet';
        $monthText['August'] = 'Août';
        $monthText['September'] = 'Septembre';
        $monthText['October'] = 'Octobre';
        $monthText['November'] = 'Novembre';
        $monthText['December'] = 'Décembre';
        
        return $monthText;
    }
    
    public function echoMonthtoShortFr() {
        $monthText['January'] = 'Jan';
        $monthText['February'] = 'Fév';
        $monthText['March'] = 'Mar';
        $monthText['April'] = 'Avr';
        $monthText['May'] = 'Mai';
        $monthText['June'] = 'Jui';
        $monthText['July'] = 'Juil';
        $monthText['August'] = 'Aoû';
        $monthText['September'] = 'Sep';
        $monthText['October'] = 'Oct';
        $monthText['November'] = 'Nov';
        $monthText['December'] = 'Déc';
        
        return $monthText;
    }
    
    public function echoMonthtoEn() {
        $monthText['Janvier'] = 'January';
        $monthText['Février'] = 'February';
        $monthText['Mars'] = 'March';
        $monthText['Avril'] = 'April';
        $monthText['Mai'] = 'May';
        $monthText['Juin'] = 'June';
        $monthText['Juillet'] = 'July';
        $monthText['Août'] = 'August';
        $monthText['Septembre'] = 'September';
        $monthText['Octobre'] = 'October';
        $monthText['Novembre'] = 'November';
        $monthText['Décembre'] = 'December';
        
        return $monthText;
    }
}

